Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • "{ is required" in a loop

    Hi,

    I wrote a really simple syntax for a loop. Code is here.

    Code:
    gen householderID=.
    forval i=1/15 {
    replace householderID=Q103ID`i' 
    if Q103a`i'==1 
    }
    Code:
    gen householderID=.
     foreach i of num 1/15 {
                 replace householderID=Q103ID`i' 
                 if Q103a`i'==1 
            }
    No matter what syntax I use, forv or foreach, Stata says "{ is required".
    I searched on the web and someone indicates that there are something preventing Stata from reading the "{". But I don't think mine is in line with that.
    So what on earth is the problem ?

    Maybe that's a little stupid but I really want to figure out what Stata is thinking.

    ​​​​​​​Thank you very much.


  • #2
    Stata is reading the { at the end of the -forval- and -foreach- command lines. The problem is that you have put your -if- conditions on a separate line from the command they modify and you have not told Stata to continue the -replace- command on the next line. So Stata thinks that what you intend as an -if- condition restricting the -replace- command is an -if- command starting on the next line,* and an -if- command must end with {. Just put your -if- conditions back on the same line as the -replace- and you will be fine. Or, tell Stata that the -replace- command continues on the next line by putting /// at the end of the -replace- line.

    *An -if- command is very different from an -if- condition. If you are not familiar with -if- commands read -help ifcmd-. I'm inferring from the whole block of code that you do not want an -if- command here. Rather I'm inferring that you want the -replace- command to operate only on those observatoins in which Q103a`i' == 1. That requires an -if- qualifier, and you can fix the problem by moving it up to the same line as the -replace- statement, as suggested above.

    If that's not what you want, and you really want to start an -if- command here, then you have to supply a { at the end of that line, and then write at least one line of code telling Stata what to do to the entire data set in the event that the value of Q103a`i' in the first observation is 1, and then another line with just a }.

    Comment


    • #3
      Thank you Clyde.
      That's...embarrassed. I thought there was something wrong with my loop syntax. However, it's just about -if-. I thought I knew the -if- condition well (it is used a lot after all) . Actually, I didn't. That's a stupid mistake but it reminds me that I need to pay more attention to details and to know more about Stata, which is a good thing.

      Trying to be good friend with Stata.

      Comment


      • #4
        Dear Clyde,

        I have the same issue here with this command,
        would you please help

        * Renaming variables
        foreach i in hidp lmary4 lmcoh lmcby4 lmwwy4 lspwwd lmdvy4 lcsby4 lcsey4 leshsy4 ///
        spellno leshs pj1soc10_cc lacby4{
        ren i_`i' `i'

        }

        Comment


        • #5
          Show exactly what Stata returned to you when you typed what you typed. I do not see any errors in what you are showing so far.

          Also check out [D] rename group -- Rename groups of variables. You do not need a loop for what you are doing.


          Originally posted by Yara Issa View Post
          Dear Clyde,

          I have the same issue here with this command,
          would you please help

          * Renaming variables
          foreach i in hidp lmary4 lmcoh lmcby4 lmwwy4 lspwwd lmdvy4 lcsby4 lcsey4 leshsy4 ///
          spellno leshs pj1soc10_cc lacby4{
          ren i_`i' `i'

          }

          Comment


          • #6
            Originally posted by Yara Issa View Post
            Dear Clyde,

            I have the same issue here with this command,
            would you please help

            * Renaming variables
            foreach i in hidp lmary4 lmcoh lmcby4 lmwwy4 lspwwd lmdvy4 lcsby4 lcsey4 leshsy4 ///
            spellno leshs pj1soc10_cc lacby4{
            ren i_`i' `i'

            }
            Your codes means that you have i_hidp i_Imary4 i_Imcoh...in your data.Please check if these variables are in your data.
            Best regards.

            Raymond Zhang
            Stata 17.0,MP

            Comment


            • #7
              Dear all,

              I am facing a similar issue and maybe someone of you can help me. I get the error message "{ required". My code looks as follows:

              local A vote*
              cap drop ideo
              gen ideo = .
              foreach y of local `A' {
              replace ideo = 0 if inrange(,0,2.9) ///
              replace ideo= 1 if inrange(3,7.9) ///
              replace ideo= 2 if inrange(8,10)
              }

              Best,
              Insa

              Comment


              • #8
                There are several different syntax and logic errors here:

                1) When Stata sees "///" at the end of a line, you are telling it that the command on that line is not complete, and it should use the material on the next line as part of the same line. Consequently, what Stata sees inside your loop is:
                Code:
                replace ideo = 0 if inrange(,0,2.9) replace ideo= 1 if inrange(3,7.9) replace ideo= 2 if inrange(8,10)
                Stata does not permit multiple commands on a line, and it gets very confused and gives that error message. You don't need *any* of those "///" because each -replace= command is complete in itself.

                2) -inrange- returns 0 or 1 depending on whether the first argument you give it is within the range of the second two. So, for example, -inrange(0,2,9)- returns a 1 if 0 is between 2 and 9, which it always is. I suspect you wanted some variable name somewhere in your inrange() function, such as perhaps:
                Code:
                replace ideo = 0 if inrange(y, 2,9)
                3) Further regarding inrange(): -help inrange()- shows that its argument list must contain three items separated by commas. *None* of your inrange() commands fit this.

                4) Your loop iterator is "y" but you never refer to it in the loop.

                So, the least of your problems is your loop syntax itself, and any one of these errors might have caused your error message.

                I can't determine how to fix your code because I only have wild guesses about what you *wanted* to do. Instead of trying to fix pieces of this code, I'd suggest that you post back an explanation of what you wanted to have happen, rather than example code. In doing this, a short example of what your data looks like and what you want it to look like might help. (If you post example data, you'll do best to follow the instructions regarding -dataex- in the StataList FAQ (tab at the top of the page in Statalist). As a new participant, you'll find various things in this FAQ useful.) I'm guessing that you don't even want or need a loop here to do what you want.

                Comment


                • #9
                  Dear Mark,

                  thank you for your detailed reply! When I use the following code (fixing the errors you mentioned) it runs smoothly:

                  local A voteAus02 voteBel02
                  foreach y in `A' {
                  replace ideo = 0 if inrange(`y', 0,2.9)
                  replace ideo = 1 if inrange(`y', 3,7.9)
                  replace ideo = 2 if inrange(`y',8,10)
                  }

                  Best,
                  Insa

                  Comment


                  • #10
                    Code:
                    foreach y in voteAus02 Bel02 {
                    would be fine. The local macro is not needed and perhaps even confusing to some readers.

                    Comment


                    • #11
                      Now that I understand what is wanted, I'd point to -recode- as an option here, and also raise a question about whether Insa's syntax will do what is wanted:

                      Code:
                      recode voteAus02 voteBel02 (0/2.9 = 0) (3/7.9 = 1 ) (8/10 = 2), prefix(ideo_)
                      This will create two new variables, ideo_voteAus02 and ideo_voteBel02. I suggest this because I suspect that Insa didn't want to create one new variable based on the value of more than one original variable. As shown in #9 above, the loop would replace the value of ideo more than once, i.e., once for each vote* variable, and I don't think that was intended. Some attention to the endpoints of the range is necessary here (i.e., what if 2.95 is a possible value for voteAus02), but that was true of the -replace- based syntax, too.

                      Comment

                      Working...
                      X